﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Documents;
using System.Windows.Forms;
using System.Xml;
using DataAccess;
using Model;

namespace Hazop
{
    public partial class HazopTeamPage : UserControl{
        private static int _id = 0;
        private static int _lastColumns = 0;
        public HazopTeamPage()
        {
            InitializeComponent();
            BindData();
        }

        private void BindData(){
            DgvHazopTeam.Enabled = true;
            List<HazopTeam> hazopTeams = Program.Workspace.project.HazopTeam;
            BindingList<HazopTeam> hazopTeamList = new BindingList<HazopTeam>(hazopTeams);
            dgvHazopTeam.DataSource = hazopTeamList;
            if(hazopTeamList.Count > 0 && hazopTeamList[0].DateList.Count > 0){
                for (int i = 0; i < hazopTeamList[0].DateList.Count; i++){
                    MDataGridViewCalendarColumn calendarColumn = new MDataGridViewCalendarColumn();
                    calendarColumn.HeaderText = "日期";
                    calendarColumn.Name = i.ToString();
                    dgvHazopTeam.Columns.Add(calendarColumn);  
                }
                int columns = hazopTeamList[0].DateList.Count;
                int rows = hazopTeams.Count;
                for (int i = 0; i < rows; i++){
                    for (int j = 0; j < columns; j++){
                        dgvHazopTeam.Rows[i].Cells[j.ToString()].Value = hazopTeams[i].DateList[j];
                    }
                }
            }
           
        }

        private void btnEdit_Click(object sender, EventArgs e){
            dgvHazopTeam.Enabled = true;
            _lastColumns = dgvHazopTeam.Columns.Count - 4;
            MDataGridViewCalendarColumn calendarColumn = new MDataGridViewCalendarColumn();
            calendarColumn.HeaderText = "日期";
            calendarColumn.Name = _id.ToString();
            _id++;
            dgvHazopTeam.Columns.Add(calendarColumn);  
        }

        private List<string> GetTitleList(){
            List<string> titleList = new List<string>();
            XmlNodeList nodeList = XMLHelper.GetXmlNodeListByXpath("./Config/HazopTeam.xml","/HazopTeam/Title");
            foreach (XmlNode node in nodeList){
                titleList.Add(node.InnerText);
            }
            return titleList;
        }

        private void BindTitleColumnToolTips(){
            List<string> titleList = GetTitleList();
            string toolTips = string.Empty;
            foreach (string s in titleList){
                toolTips += (s + '\n');
            }
            dgvHazopTeam.CurrentRow.Cells[3].ToolTipText = toolTips;
        }

        private void dgvHazopTeam_CellBeginEdit(object sender, DataGridViewCellCancelEventArgs e){
            BindTitleColumnToolTips();
        }

        private void dgvHazopTeam_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
        {
            Rectangle rectangle = new Rectangle(e.RowBounds.Location.X, e.RowBounds.Location.Y, dgvHazopTeam.RowHeadersWidth - 4, e.RowBounds.Height);
            TextRenderer.DrawText(e.Graphics, (e.RowIndex + 1).ToString(), dgvHazopTeam.RowHeadersDefaultCellStyle.Font, rectangle,
                dgvHazopTeam.RowHeadersDefaultCellStyle.ForeColor, TextFormatFlags.VerticalCenter | TextFormatFlags.Right);
        }

        private void dgvHazopTeam_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
        {
            if (e == null || e.Control == null || e.Control.GetType() != typeof(DataGridViewTextBoxEditingControl))
                return;
            DataGridViewTextBoxEditingControl te = (DataGridViewTextBoxEditingControl)e.Control;
            te.AutoCompleteMode = AutoCompleteMode.SuggestAppend; //建议追加 
            te.AutoCompleteSource = AutoCompleteSource.CustomSource;
            if (dgvHazopTeam.CurrentCell.OwningColumn.Name.Equals("colTitle"))
            {
                List<string> remindTitleList = new List<string>();
                remindTitleList = GetTitleList();
                te.AutoCompleteCustomSource.AddRange(GetRemindTitleList(remindTitleList));
                //  GetRemindReasonList(remindReasonList);
            }
        }

        private string[] GetRemindTitleList(List<string> reasonList)
        {
            string[] result = new string[reasonList.Count];
            for (int i = 0; i < reasonList.Count; i++)
            {
                result[i] = reasonList[i];
            }
            return result;
        }

        public void btnSave_Click(object sender, EventArgs e){          
            List<HazopTeam> hazopTeams = new List<HazopTeam>((BindingList<HazopTeam>)dgvHazopTeam.DataSource);

            int columns = dgvHazopTeam.Columns.Count - _lastColumns - 4;
            int rows = dgvHazopTeam.Rows.Count;
            dgvHazopTeam.Enabled = false;
            if (columns < 1 || rows < 1){
                Program.Workspace.project.HazopTeam = hazopTeams;
                Program.Workspace.SaveProject(MainForm.mainForm.SaveFilePath);
                MessageBox.Show("Hazop小组保存成功!");
                return;
            }
//            if (Program.Workspace.project.HazopTeam[0].DateList.Count == 0){
//                MessageBox.Show("Hazop小组保存成功!");
//                return;
//            }
            for (int i = 0; i < rows - 1; i++){
                for (int j = _lastColumns; j < columns+_lastColumns; j++){
                    string s = Convert.ToDateTime(dgvHazopTeam.Rows[i].Cells[j].Value).ToShortDateString();
                    string s1 = (dgvHazopTeam.Rows[i].Cells[j.ToString()].Value).ToString();
                    string s2 = (dgvHazopTeam.Rows[i].Cells[0].Value).ToString();
                    string s3 = (dgvHazopTeam.Rows[i].Cells[1].Value).ToString();
                    string s4 = (dgvHazopTeam.Rows[i].Cells[2].Value).ToString();
                    string s5 = (dgvHazopTeam.Rows[i].Cells[3].Value).ToString();
                    string s6 = (dgvHazopTeam.Rows[i].Cells[4].Value).ToString();
                    Program.Workspace.project.HazopTeam[i].DateList.Add(s);
                }
            }
            Program.Workspace.project.HazopTeam = hazopTeams;
            Program.Workspace.SaveProject(MainForm.mainForm.SaveFilePath);
            MessageBox.Show("Hazop小组保存成功!");
        }

        private void btnModify_Click(object sender, EventArgs e){
            dgvHazopTeam.Enabled = true;

            List<HazopTeam> hazopTeams = Program.Workspace.project.HazopTeam;
      //      BindingList<HazopTeam> hazopTeamList = new BindingList<HazopTeam>(hazopTeams);
         //   dgvHazopTeam.DataSource = hazopTeamList;
            if (hazopTeams.Count > 0 && hazopTeams[0].DateList.Count > 0)
            {
                int columns = hazopTeams[0].DateList.Count;
                int rows = hazopTeams.Count;
                for (int i = 0; i < rows; i++){
                    for (int j = 0; j < columns; j++){
                         dgvHazopTeam.Rows[i].Cells[j.ToString()].Value = hazopTeams[i].DateList[j];
                    }
                }
            }
        }

        public DataGridView DgvHazopTeam{
            get { return dgvHazopTeam; }
            set { dgvHazopTeam = value; }
        }

        public void SaveWithoutRemider()
        {
            for (int i = 0; i < dgvHazopTeam.Rows.Count; i++)
            {
                for (int j = 0; j < 50; j++)
                {
                    if (dgvHazopTeam.Rows[i].Cells[j].Value == null)
                    {
                        break;
                    }
                    string tmp = Convert.ToDateTime(dgvHazopTeam.Rows[i].Cells[j.ToString()].Value).ToShortDateString();
                    if (Program.Workspace.project.HazopTeam[i].DateList[j].Equals(""))
                    {
                        Program.Workspace.project.HazopTeam[i].DateList[j] = tmp;
                    }
                }
            }
            dgvHazopTeam.Enabled = false;
        }
    }
}
